﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace Arbetsfornedringen.Pages
{
    public partial class Employer : System.Web.UI.Page
    {
        private List<int> workIds = new List<int>(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateGridView();
            Calendar1.SelectedDate = DateTime.Now.AddMonths(2);
            LabelStatus.Text = "";
        }

        private void PopulateGridView()
        {
            Account account = Session["Account"] as Account;
            if (account != null)
            {
                GridView1.DataSource = DAL.EmployerDB.GetWorkAdsByAccountId(account.AccountId);
                GridView1.DataBind();
            }
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            DateTime date = DateTime.Now;
            if (e.Day.Date <= date)
            {
                e.Day.IsSelectable = false;
                e.Cell.ForeColor = System.Drawing.Color.Gray;
            }
            else
            {
                e.Cell.BackColor = System.Drawing.Color.DarkSlateGray;
            }
        }

        protected void ButtonAddWorkAd_Click(object sender, EventArgs e)
        {
            Account account = Session["Account"] as Account;
            if (account != null)
            {
                WorkAds workAd = new WorkAds()
                {
                    AccountId = account.AccountId, 
                    Deleted = false, 
                    Filled = false, 
                    Title = TextBoxTitle.Text,
                    Description = TextBoxDescription.Text,
                    EnteredDate = DateTime.Now, 
                    ExpiredDate = Calendar1.SelectedDate
                };

                DAL.EmployerDB.AddWorkAd(workAd);
                LabelStatus.Text = "Klart!";
                TextBoxTitle.Text = "";
                TextBoxDescription.Text = "";
                PopulateGridView();
                
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = int.Parse(e.CommandArgument.ToString());
            int workId = int.Parse(GridView1.Rows[index].Cells[0].Text);

            if (e.CommandName == "FillWorkAd")
            {
                DAL.EmployerDB.FillWorkAd(workId);
                PopulateGridView();
            }

            if(e.CommandName == "DeleteWorkAd")
            {
                DAL.EmployerDB.DeleteWorkAd(workId);
                PopulateGridView();
            }
            
        }
    }
}